/**
 * type 0 localStorage, 1 sessionStorage , default 0
 *
 */
export default class Storage {
	static PREFIX_KEY = 'EDITOR_';
	static types = ['localStorage', 'sessionStorage'];

	static get<T>(key: string, type: number = 0): T | any {
		let result: string | undefined | null = window[this.types[type]].getItem(`${this.PREFIX_KEY}${key}`);
		if (result) {
			try {
				return JSON.parse(result);
			} catch (error) {
				return result;
			}
		}
		return null;
	}

	static set<T = any>(key: string, value: T, type: number = 0): void {
		let v: T | any = value;
		if (value instanceof Object) {
			v = JSON.stringify(value);
		}
		window[this.types[type]].setItem(`${this.PREFIX_KEY}${key}`, v);
	}

	static delete(key: string, type: number = 0) {
		window[this.types[type]].removeItem(`${this.PREFIX_KEY}${key}`);
	}
}
